package com.example.myapplication10.Buttons;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.res.ResourcesCompat;

import com.example.myapplication10.R;

public class Btn_history extends LinearLayout {
    String str = "";
    String code = "";

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    TextView tvB;
    Context context;AttributeSet attrs;

    //+"良"+" "+"AQI"+"  ";
    public Btn_history(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
//        EventBus.getDefault().register(this);
        init(context, attrs);
        this.context = context;
        this.attrs = attrs;
    }

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    public void init(Context context, @Nullable AttributeSet attrs){
        setOrientation(LinearLayout.VERTICAL);
        LinearLayout btLayout = new LinearLayout(context);
        LayoutParams btLp = new LayoutParams
                (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
//        btLp.setMargins(0,DisplayUtil.dip2px(context, 15),0,DisplayUtil.dip2px(context, 17));
        btLayout.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.button_mini_history_initstyle, null));
        btLayout.setGravity(Gravity.CENTER);
        tvB = new TextView(context);
//        tvB.setText("11111");
        tvB.setGravity(Gravity.CENTER);
        tvB.setTextAlignment(TEXT_ALIGNMENT_CENTER);
        tvB.setTextSize(14);
        tvB.setTextColor(0xffffffff);
        btLayout.addView(tvB);
        this.addView(btLayout, btLp);
    }


    public void update(Context context, @Nullable AttributeSet attrs){
        setOrientation(LinearLayout.VERTICAL);
        LinearLayout btLayout = new LinearLayout(context);
        LayoutParams btLp = new LayoutParams
                (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
//        btLp.setMargins(0,DisplayUtil.dip2px(context, 15),0,DisplayUtil.dip2px(context, 17));
        btLayout.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.button_mini_style, null));
        btLayout.setGravity(Gravity.CENTER);
        tvB = new TextView(context);
        tvB.setText(str);
        tvB.setGravity(Gravity.CENTER);
        tvB.setTextAlignment(TEXT_ALIGNMENT_CENTER);
        tvB.setTextSize(14);
        tvB.setTextColor(0xffffffff);
        btLayout.addView(tvB);
        this.addView(btLayout, btLp);

    }

    public void updateHistory(String city) {
        this.str = city;
        this.removeAllViews();
        update(this.context,this.attrs);

    }
}
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void messageEventBusAQI(MsgEventAQI event) {
//        int AQI = event.getMsg();
//        this.AQI = AQI;
////        Weather weather = new Weather();
////        CityInfoDTO cityInfo = new CityInfoDTO();
////        DataDTO data = new DataDTO();
////        YesterdayDTO yesterday = new YesterdayDTO();
////        List<ForecastDTO> forecastArrayList = new ArrayList<>();
//
//
//    }


//    public Icon(Context context, @Nullable AttributeSet attrs) {
//        super(context, attrs);
//        setOrientation(LinearLayout.VERTICAL);
//        LinearLayout topLayout = new LinearLayout(context);
//        LinearLayout bottomLayout = new LinearLayout(context);
//        LinearLayout.LayoutParams topLp = new LinearLayout.LayoutParams
//                (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,2);
//        LinearLayout.LayoutParams bottomLp = new LinearLayout.LayoutParams
//                (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,5);
//        topLayout.setBackground(ResourcesCompat.getDrawable(getResources(),R.drawable.top_style,null));
//        bottomLayout.setBackground(ResourcesCompat.getDrawable(getResources(),R.drawable.buttom_style,null));
//        TextView tvM = new TextView(context);
//        TextView tvD = new TextView(context);
//        Calendar date = Calendar.getInstance();
//        tvM.setText(String.valueOf(date.get(Calendar.MONTH)+1)+"月");
//        tvM.setTextSize(DisplayUtil.dip2px(context,30));
//        tvM.setTextColor(0xffffffff);
//        tvD.setText(String.valueOf(date.get(Calendar.DAY_OF_MONTH)));
//        tvD.setTextSize(DisplayUtil.dip2px(context,70));
//        tvD.setTextColor(0xff000000);
//        this.addView(topLayout,topLp);
//        this.addView(bottomLayout,bottomLp);
//        topLayout.addView(tvM);
//        bottomLayout.addView(tvD);
//        topLayout.setGravity(Gravity.CENTER);
//        bottomLayout.setGravity(Gravity.CENTER);
//    }